import datetime
from Crypto.Util import randpool
import hashlib


def genRandomId():
    """a tool to return random id"""
    pool = randpool.RandomPool()
    return hashlib.sha1(pool.get_bytes(1024)).hexdigest()

def infoOutput(text):
    _defaultInfoOutput(text)

def errorOutput(text):
    _defaultErrorOutput(text)
    
def warningOutput(text):
    _defaultWarningOutput(text)

def _defaultInfoOutput(text):
    print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S ") + text[:200].replace('\n','')
    
def _defaultErrorOutput(text):
    print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S ") + "ERROR " + text
    
def _defaultWarningOutput(text):
    print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S ") + "WARNING " + text

def setInfoOutput(callback):
    infoOutput = callback
    
def setErrorOutput(callback):
    errorOutput = callback

def setWarningOutput(callback):
    warningOutput = callback
    
def errMsgBox(title,text):
    dialog = gtk.MessageDialog(None,gtk.DIALOG_MODAL,gtk.MESSAGE_ERROR,gtk.BUTTONS_CLOSE,text)
    dialog.set_title(title)
    dialog.run()
    dialog.destroy()
    